package com.token.sysflow.core;


import com.token.sql.IntegrateSql;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sysflow.hander.ApproveProcess;
import com.token.sysflow.impl.AbstractFlowHandlerImpl;
import com.token.sysflow.impl.TransmitFlowImpl;

/**
 * 转交流程
 * @author chenjianxiong 020/4/13
 */
public class TransmitFlow extends AbstractFlowCore{
    public TransmitFlow(IntegrateSql f, String keyUnid) throws Exception{
        super(f,keyUnid);
    }

    @Override
    public void execute(AbstractFlowHandlerImpl flowHandler) throws Exception{
        TransmitFlowImpl transmitFlow = (TransmitFlowImpl) flowHandler;

        //检查流程状态，结束和终止不允许操作
        checkFlowStatus();

        //判断当前用户是否是当前办理人
        if (!approveProcess.isStepApproves(transmitFlow.getActionUser(), ApproveProcess.STATUS_IND)) {
            throw new FlowException(ErrorCodeEnum.CHECK_APPROVE);
        }

        //初始化起草人、申请人、当前人。
        initUserInfo(approveProcess.getCreater(),approveProcess.getApplyer(),flowHandler.getActionUser());

        //判断开关
        if(!getFromStep().isCanOther()){
            throw new FlowException(ErrorCodeEnum.CONFIG_TRANSMIT);
        }

        //设置目标流程环节
        super.setToStep(super.getFromStep());


        super.setToFlow(super.getFromFlow());


        //替换办理人
//        System.out.println("transmitFlow.getSelectApprover() = " + transmitFlow.getSelectApprover());
        approveProcess.replaceApprover(super.getFromStep(),transmitFlow.getSelectApprover()[0],transmitFlow.getActionUser());


        //添加输出参数
        approveProcess.appendFlowProcessInfo(toFlow,toStep);

        //生成新的流转记录和办理意见
        super.createFlowApproveLog(flowHandler);
    }
}
